package service

import (
	"context"

	"gitee.com/zhaokangming/tiny-ticktok/dao"
	"gitee.com/zhaokangming/tiny-ticktok/model"
	"gitee.com/zhaokangming/tiny-ticktok/pkg/app"
)

type UserListResponse struct {
	app.Response
	UserList []model.User `json:"user_list"`
}

func Follow(ctx context.Context, uid, targetID int64) error {
	err := dao.CreateFollow(ctx, uid, targetID)
	if err != nil {
		return err
	}

	return nil
}

func CancelFollow(ctx context.Context, uid, targetID int64) error {
	err := dao.DeleteFollow(ctx, uid, targetID)
	if err != nil {
		return err
	}

	return nil
}

func GetFollowOrFans(ctx context.Context, key string, uid, targetID int64) (*UserListResponse, error) {
	ids, err := dao.MGetFollowOrFansIDs(ctx, key, targetID)
	if err != nil {
		return nil, err
	}

	users, err := dao.MGetUsers(ctx, ids)
	if err != nil {
		return nil, err
	}

	res := new(UserListResponse)
	res.UserList = make([]model.User, len(users))
	for i, v := range users {
		res.UserList[i].Id = int64(v.ID)
		res.UserList[i].Name = v.Name
		QueryFollowInfo(ctx, &res.UserList[i], uid, int64(v.ID))
	}

	return res, nil
}
